﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class News : System.Web.UI.Page
    {
        private static NewsBO newsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((Session["LOGIN"] == null) || (int)Session["ROLES"] != 1)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            if (!IsPostBack)
            {
                dldanhmuc.DataSource = CategoryInfo.GetCategoryInfo_BySingle(false, false, false, true, false);
                dldanhmuc.DataBind();
                dldanhmuc.Items.Insert(0, "Tất cả");
                GridView1.DataSource = NewsBOList.GetAllNewBOList();
                GridView1.DataBind();
            }
            string mode = Request.QueryString["Save"];
            //Mode Add
            if (mode == "Success")
            {
                messagedelete.Text = "Đã thêm thành công";
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int newsid_query = Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
            Response.Redirect("Edit_AddNews.aspx?Mode=" + newsid_query);

        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit_AddNews.aspx");
        }

        protected void btnxoatin_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int newsid = Convert.ToInt32(gvRow.Cells[3].Text);
                        newsbo = NewsBO.GetNewsBO(newsid);
                        NewsBO.DeleteNewsBO(newsid);
                    }
                    catch
                    {
                        messagedelete.Text = "Xóa thất bại";
                        return;
                    }
                }
            }
                if(test==false)
                {
                    ScriptManager.RegisterStartupScript(Page,Page.GetType(),"Success","alert('Chưa chọn thông tin cần xóa!!!)",true);
                    return;
                }
                else
                {
                    dldanhmuc_SelectedIndexChanged(sender, e);
                    messagedelete.Text = "Xóa thành công! ";
                }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }        
        }

        protected void dldanhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dldanhmuc.SelectedValue == "Tất cả")
            {
                GridView1.DataSource = NewsBOList.GetAllNewBOList();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = NewsBOList.GetNewsBOList_ByID(Convert.ToInt32(dldanhmuc.SelectedValue));
                GridView1.DataBind();
            }
            messagedelete.Text = "";
        }
        //protected void btnhuybo_Click(object sender, EventArgs e)
        //{
        //    PanelActions.Visible = false;
        //}
    }
}
